package com.irate.activity;

import org.apache.http.Header;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;
import com.irate.dao.UserDAO;
import com.irate.fragment.AccountFragment;
import com.irate.fragment.FriendFragment;
import com.irate.fragment.HomeFragment;
import com.irate.fragment.RatingFragment;
import com.irate.fragment.SettingFragment;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.provider.ContentManager;

public class MainActivity extends FragmentActivity {

	protected TextView home;
	protected TextView friend;
	protected TextView account;
	protected TextView setting;
	protected ImageView r_button;
	protected TextView site_title;

	protected FrameLayout mContent;
	private static long back_pressed;

	protected UserDAO userDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init();
		excute();
	}

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this);
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {

		if (back_pressed + 2000 > System.currentTimeMillis()) {
			super.onBackPressed();

			userDao.logout(handlerLogout, MainActivity.this,null);

			System.exit(0);
		} else
			Toast.makeText(getBaseContext(),
					getResources().getString(R.string.closeapp),
					Toast.LENGTH_SHORT).show();

		back_pressed = System.currentTimeMillis();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		try {
			userDao.logout(handlerLogout, MainActivity.this, null);
		} catch (Exception e) {

		}
	}

	private void init() {

		home = (TextView) findViewById(R.id.home);
		friend = (TextView) findViewById(R.id.friend);
		account = (TextView) findViewById(R.id.account);
		setting = (TextView) findViewById(R.id.setting);
		r_button = (ImageView) findViewById(R.id.r_button);
		site_title = (TextView) findViewById(R.id.site_title);

		userDao = new UserDAO();
		mContent = (FrameLayout) findViewById(R.id.content_main);
	}

	private void excute() {
		home.setOnClickListener(homeClick);
		friend.setOnClickListener(friendClick);
		account.setOnClickListener(accountClick);
		setting.setOnClickListener(settingClickListener);
		r_button.setOnClickListener(rating_click);

		site_title.setText(getString(R.string.home));
		home.setSelected(true);

		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_main, new HomeFragment()).commit();
	}

	OnClickListener rating_click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			home.setSelected(false);
			friend.setSelected(false);
			account.setSelected(false);
			setting.setSelected(false);

			site_title.setText(getString(R.string.home));
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_main, new RatingFragment()).commit();
		}
	};
	
	OnClickListener homeClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			v.setSelected(true);
			friend.setSelected(false);
			account.setSelected(false);
			setting.setSelected(false);

			site_title.setText(getString(R.string.home));
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_main, new HomeFragment()).commit();
		}
	};

	OnClickListener friendClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			v.setSelected(true);
			home.setSelected(false);
			account.setSelected(false);
			setting.setSelected(false);
			
			site_title.setText(getString(R.string.friend));
			
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_main, new FriendFragment()).commit();
		}
	};

	OnClickListener accountClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			v.setSelected(true);
			home.setSelected(false);
			friend.setSelected(false);
			setting.setSelected(false);
			
			site_title.setText(getString(R.string.my_account));
			
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_main, new AccountFragment()).commit();
		}
	};

	OnClickListener settingClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			v.setSelected(true);
			home.setSelected(false);
			friend.setSelected(false);
			account.setSelected(false);
			
			site_title.setText(getString(R.string.setting));
			
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_main, new SettingFragment()).commit();
		}
	};

	AsyncHttpResponseHandler handlerLogout = new AsyncHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] response) {
			System.out.println("Logout Success");
			// finish();
			ContentManager.getInstance().isLogin(false);
		}

		@Override
		public void onStart() {
			System.out.println("Start");
		}

		@Override
		public void onFinish() {
			// if (dialog != null)
			// dialog.dismiss();
		}

		@Override
		public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
			System.out.println("Fail Register");
			// try {
			// JSONObject obj = new JSONObject(response);
			//
			// Toast.makeText(getActivity(),
			// obj.getString("message").toString(), Toast.LENGTH_SHORT)
			// .show();
			//
			// } catch (Exception ex) {
			//
			// }
		}
	};
}
